package de.velvettear.squirrel.util;

import java.io.File;
import java.io.FileOutputStream;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.PictureCallback;
import android.os.Handler;
import android.util.Log;
import android.view.SurfaceView;
import de.velvettear.squirrel.kernel.Statics;

public class CameraHelper {
	
	private static Camera camera;
	private static Handler handler;
	private static String command;
	private static PictureHandler pictureHandler;
	
	private static void loadVariables() {
		if (pictureHandler == null) {
			pictureHandler = new PictureHandler();
		}
	}
	
	public static void takeFrontPicture(Context context, Handler handler, String command) {
		loadVariables();
		
		Log.i("takeFrontPicture()", "BEGIN");
		CameraHelper.handler = handler;
		CameraHelper.command = command;
				
		try {
			getFrontCamera();
			camera.takePicture(null, null, pictureHandler);
		} catch (Exception e) {
			if (e.getMessage() != null) {
				Log.e("takeFrontPicture()", e.getMessage());
			}
		}		
		Log.i("takeFrontPicture()", "END");
	}
	
//	public static void takeBackPicture(Context context, SurfaceView preview) {
//		takeBackPicture(context, preview, null, null);
//	}
	
	public static void takeBackPicture(Context context, Handler handler, String command) {
		loadVariables();
		
		Log.i("takeBackPicture()", "BEGIN");	
		CameraHelper.handler = handler;
		CameraHelper.command = command;
		
		try {
			getBackCamera();
			camera.setPreviewDisplay(new SurfaceView(context).getHolder());		
//			camera.setDisplayOrientation(90);
			camera.startPreview();
			camera.takePicture(null, null, pictureHandler);
//			camera.stopPreview();
		} catch (Exception e) {
			if (e.getMessage() != null) {
				Log.e("takeBackPicture()", e.getMessage());
			}
		}	
		
		Log.i("takeBackPicture()", "END");	
	}
	
	public static Camera getFrontCamera() {
		Log.i("getFrontCamera()", "BEGIN");			
		if (camera == null) {
			camera = Camera.open(getFrontCameraID());
		}
		Log.i("getFrontCamera()", "END");	
		return camera;
	}
	
	public static Camera getBackCamera() {		
		Log.i("getBackCamera()", "BEGIN");	
		if (camera == null) {
			camera = Camera.open(getBackCameraID());
		}
		Log.i("getBackCamera()", "END");	
		return camera;
	}
	
	public static int getFrontCameraID() {
		Log.i("getFrontCameraID()", "BEGIN");		
		
		int id = -1;		
		int camNumber = Camera.getNumberOfCameras();
		for (int number = 0; number < camNumber; number++) {
			CameraInfo info = new CameraInfo();
			Camera.getCameraInfo(number, info);
			if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
				Log.i("getFrontCameraID()", "FRONT CAMERA ID: " + number);
				id =  number;
			}
		}
		
		Log.i("getFrontCameraID()", "END");		
		return id;		
	}
	
	public static int getBackCameraID() {
		Log.i("getBackCameraID()", "BEGIN");		
		
		int id = -1;
		int camNumber = Camera.getNumberOfCameras();
		for (int number = 0; number < camNumber; number++) {
			CameraInfo info = new CameraInfo();
			Camera.getCameraInfo(number, info);
			if (info.facing == CameraInfo.CAMERA_FACING_BACK) {
				Log.i("getBackCameraID()", "BACK CAMERA ID: " + number);
				id = number;
			}
		}
		
		Log.i("getBackCameraID()", "END");		
		return id;		
	}
		
	public static void resetCamera() {
		if (camera != null) {
			camera.stopPreview();
			camera.release();
			camera = null;
		}
	}
		
	public static class PictureHandler implements PictureCallback {

		public PictureHandler() {
			
		}
		
		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
			File picture = new File(Statics.PATH_PICTURES + File.separator + TimeHelper.getFileDate() + ".jpg");
			
			try {
				FileOutputStream out = new FileOutputStream(picture);
				out.write(data);
				out.flush();
				out.close();
			} catch (Exception e) {
				if (e.getMessage() != null) {
					Log.e("onPictureTaken()", e.getMessage());
				}
			}
				
//			resetCamera();
			
			if (handler != null && command != null) {
				handler.sendMessage(MessageHelper.createMessage(Statics.HANDLER_PICTURE, picture.getAbsolutePath(), command));
			}
		}
		
	}
	
}
